home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / hypercrd / xcmds / rtf-redr.hqx / FieldToRTF.c next >
C/C++ Source or Header  |  1992-12-19  |  6KB  |  261 lines

  1. /*
  2.  * This software is copyright 1992 by Robert Morris.
  3.  * You may freely redistribute this software as shareware
  4.  * if you do so in the same form as you got it. If you find
  5.  * this software useful, please send $12 to:
  6.  *   Robert Morris
  7.  *   P.O. Box 1044
  8.  *   Harvard Square Station
  9.  *   Cambridge, MA 02238
  10.  *   ecognome@aol.com
  11.  * If you incorporate any of this software in any kind of
  12.  * commercial product, please send $2 per copy distributed
  13.  * to the above address.
  14.  */
  15.  
  16. /*
  17.  * FieldToStyled(cardflag, fieldname)
  18.  * yields RTF for the field.
  19.  */
  20.  
  21. #include <HyperXCmd.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include "SetUpA4.h"
  25. #include <string.h>
  26. #include "lists.h"
  27.  
  28. int sprintf(char *, const char *, ...);
  29. int sscanf(const char *, const char *, ...);
  30.  
  31. Handle HStr(char *);
  32. int **addfonttable();
  33. char *fontfamily();
  34.  
  35. pascal void
  36. main(paramPtr)
  37. XCmdPtr paramPtr;
  38. {
  39.     char **in;
  40.     int run, len, c;
  41.     long i, inlen;
  42.     TEHandle teh;
  43.     char fieldname[256], tmp[256], fname[256];
  44.     Boolean cardFld;
  45.     STElement *stp;
  46.     TEStyleHandle sh;
  47.     struct list out;
  48.     int **fonttable, nfonts;
  49.     char *dummy;
  50.     
  51.     RememberA0();
  52.     SetUpA4();
  53.     
  54.     dummy = "Copyright 1991 by Differential Development.";
  55.     
  56.     if(paramPtr->paramCount != 2){
  57.         paramPtr->returnValue = HStr("error : usage FieldToRTF(cardflag, fieldname)\rCopyright 1992 Robert Morris.");
  58.         RestoreA4();
  59.         return;
  60.     }
  61.     
  62.     strncpy(fieldname, *(paramPtr->params[1]), sizeof(fieldname)-1);
  63.     fieldname[253] = '\0';    /* seems to be the max field name length */
  64.     CtoPstr(fieldname);
  65.     
  66.     if(strcmp(*(paramPtr->params[0]), "false") == 0)
  67.         cardFld = FALSE;
  68.     else
  69.         cardFld = TRUE;
  70.     
  71.     teh = GetFieldTE(paramPtr, cardFld, 0, 0, (StringPtr)fieldname);
  72.     if(teh == 0 || paramPtr->result != xresSucc){
  73.         paramPtr->returnValue = HStr("error : no such field");
  74.         RestoreA4();
  75.         return;
  76.     }
  77.  
  78.     sh = GetStylHandle(teh);
  79.     if(sh == 0){
  80.         TEDispose(teh);
  81.         paramPtr->returnValue = HStr("error : bad GetStylHandle()");
  82.         RestoreA4();
  83.         return;
  84.     }
  85.     
  86.     in = (char **)TEGetText(teh);
  87.     if(in == 0){
  88.         TEDispose(teh);
  89.         paramPtr->returnValue = HStr("error : bad TEGetText()");
  90.         RestoreA4();
  91.         return;
  92.     }
  93.     
  94.     NewList((char **) 0, &out);
  95.     
  96.     AppendList(&out, "{\\rtf1\\mac");
  97.     
  98.     /*  figure out which fonts are used */
  99.     fonttable = 0;
  100.     for(run = 0; run < (*sh)->nRuns; run++){
  101.         stp = *((*sh)->styleTab) + ((*sh)->runs)[run].styleIndex;
  102.         if(findfonttable(fonttable, stp->stFont) < 0){
  103.             fonttable = addfonttable(fonttable, stp->stFont);
  104.             if(fonttable == 0){
  105. oom:
  106.                 TEDispose(teh);
  107.                 paramPtr->returnValue = HStr("error : out of memory");
  108.                 RestoreA4();
  109.                 return;
  110.             }
  111.         }
  112.     }
  113.     if(fonttable == 0){
  114.         fonttable = addfonttable(fonttable, 0);
  115.         if(fonttable == 0)
  116.             goto oom;
  117.     }
  118.         
  119.     /* spit out the list of fonts, using the Mac font numbers */
  120.     sprintf(tmp, "\\deff%u{\\fonttbl", (*fonttable)[0]);
  121.     AppendList(&out, tmp);
  122.     nfonts = GetHandleSize((Handle)fonttable) / sizeof(**fonttable);
  123.     for(i = 0; i < nfonts; i++){
  124.         GetFontName((*fonttable)[i], (StringPtr)fname);
  125.         PtoCstr(fname);
  126.         if(fname[0] == '\0')
  127.             sprintf(fname, "x%d", (*fonttable)[i]);
  128.         sprintf(tmp, "{\\f%u\\f%s %s;}",
  129.                 (*fonttable)[i], fontfamily((*fonttable)[i]), fname);
  130.         AppendList(&out, tmp);
  131.     }
  132.     AppendList(&out, "}\r");
  133.     DisposHandle((Handle)fonttable);
  134.     
  135.     run = -1;
  136.     inlen = (*teh)->teLength;
  137.     for(i = 0; i < inlen; i++){
  138.         if(run+1 < (*sh)->nRuns && i >= ((*sh)->runs)[run+1].startChar){
  139.             run++;
  140.             stp = *((*sh)->styleTab) + ((*sh)->runs)[run].styleIndex;    
  141.                 
  142.             /* reset to plain style, then set the font and size */
  143.             sprintf(tmp, "\\plain\\f%u\\fs%d",
  144.                     stp->stFont, stp->stSize * 2);
  145.             
  146.             if(stp->stFace & bold)
  147.                 strcat(tmp, "\\b");
  148.             if(stp->stFace & italic)
  149.                 strcat(tmp, "\\i");
  150.             if(stp->stFace & underline)
  151.                 strcat(tmp, "\\ulw");
  152.             if(stp->stFace & outline)
  153.                 strcat(tmp, "\\outl");
  154.             if(stp->stFace & shadow)
  155.                 strcat(tmp, "\\shad");
  156.             if(stp->stFace & 0x80)    /* HyperCard's grouped text */
  157.                 strcat(tmp, "\\hcgroup");
  158. #if 0
  159.             if(stp->stFace & condense)
  160.                 strcat(sname, "condense,");
  161.             if(stp->stFace & extend)
  162.                 strcat(sname, "extend,");
  163. #endif
  164.             strcat(tmp, " ");
  165.             AppendList(&out, tmp);
  166.         }
  167.         
  168.         c = (*in)[i];
  169.         if(c >= ' ' && c < 0177 && c != '\\' && c != '{' && c != '}'){
  170.             AppendListChar(&out, c);
  171.         } else if(c == '\r'){
  172.             AppendList(&out, "\\par\r");
  173.         } else {
  174.             sprintf(tmp, "\\'%02x", c & 0xff);
  175.             AppendList(&out, tmp);
  176.         }
  177.     }
  178.  
  179.     AppendList(&out, "}");
  180.     
  181.     TEDispose(teh);
  182.     
  183.     TrimList(&out);
  184.     if(out.h == 0){
  185.         paramPtr->returnValue = HStr("error : out of memory");
  186.         RestoreA4();
  187.         return;
  188.     }
  189.     
  190.     paramPtr->returnValue = out.h;
  191.         
  192.     RestoreA4();
  193. }
  194.  
  195. Handle
  196. HStr(str)
  197. char *str;
  198. {
  199.     Handle newHndl;
  200.     
  201.     newHndl = (Handle) NewHandle((long) strlen(str) + 1);
  202.     if(newHndl == 0)
  203.         return(0);
  204.     strcpy((char *) (*newHndl), str);
  205.     return(newHndl);
  206. }
  207.  
  208. int
  209. findfonttable(tb, f)
  210. int **tb;    /* a table of fonts for RTF */
  211. int f;        /* the Mac font # */
  212. {
  213.     int n, i;
  214.     
  215.     n = GetHandleSize((Handle)tb) / sizeof(int);
  216.     for(i = 0; i < n; i++)
  217.         if(f == (*tb)[i])
  218.             return(i);
  219.     return(-1);
  220. }
  221.  
  222. int **
  223. addfonttable(tb, f)
  224. int **tb;
  225. int f;    /* the Mac font # */
  226. {
  227.     int n;
  228.     
  229.     if(tb == 0){
  230.         tb = (int **)NewHandle(0L);
  231.         if(tb == 0)
  232.             return(0);
  233.     }
  234.     
  235.     n = GetHandleSize((Handle)tb) / sizeof(**tb);
  236.     SetHandleSize((Handle)tb, (n + 1) * sizeof(**tb));
  237.     if(MemError() != 0){
  238.         DisposHandle((Handle)tb);
  239.         return(0);
  240.     }
  241.     (*tb)[n] = f;
  242.     return(tb);
  243. }
  244.  
  245. char *
  246. fontfamily(f)
  247. int f;
  248. {
  249.     switch(f){
  250.     case 0: case geneva: case helvetica:
  251.         return("swiss");
  252.     case newYork: case times:
  253.         return("roman");
  254.     case monaco: case courier:
  255.         return("modern");
  256.     case symbol:
  257.         return("tech");
  258.     default:
  259.         return("nil");
  260.     }
  261. }